package com.wang.code.web;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.github.kevinsawicki.http.HttpRequest;
import com.wang.code.dao.JijinRepository;
import com.wang.code.entity.JijinDO;
import com.wang.code.service.JIJinService;
import com.wang.constants.SystemParam;
import com.wang.entity.JijinVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author wanglei
 * @desc
 * @date 2018/12/6
 * @see
 * @since 1.0
 */
@RestController
public class JijinContorller {

    @Autowired
    private JijinRepository jijinRepository;
    @Autowired
    private JIJinService jiJinService;

    public JijinContorller() {
    }


    @GetMapping("/reptile/lsjz")
    public String reptile(String code) {
        jiJinService.reptileLSJZ(code, 1);
        return "ok";
    }

    @PostMapping("/create")
    public JijinVO info(@RequestParam String code) {
        JijinVO save = jiJinService.save(code);
        return save;
    }

    @GetMapping("/list")
    public Map<String, Object> list() {
        List<JijinDO> all = jijinRepository.findAll();
        BigDecimal zero = BigDecimal.ZERO;
        for (JijinDO jijinDO : all) {
            String format = StrUtil.format("<a target=\"_blank\" href=\"{}\">{}</a>", SystemParam.tianUrl(jijinDO.getCode()), jijinDO.getCode());
            jijinDO.setCode(format);
            if (jijinDO.getShare() != null && jijinDO.getPrice() != null) {
                BigDecimal m1 = jijinDO.getShare().multiply(jijinDO.getPrice());
                BigDecimal m2 = jijinDO.getShare().multiply(jijinDO.getGsz());
                BigDecimal subtract = m2.subtract(m1);
                jijinDO.setEstimate(String.format("%.2f", subtract));
                BigDecimal multiply = subtract.divide(m1, 4, RoundingMode.HALF_UP).multiply(new BigDecimal(100));
                jijinDO.setEstimatePercent(String.format("%.2f", multiply) + "%");
                zero = zero.add(subtract);
            }
        }

        all = all.stream().sorted((o1, o2) -> {
            double d1 = Double.parseDouble(o1.getGszzl());
            double d2 = Double.parseDouble(o2.getGszzl());
            if (d1 > d2) {
                return -1;
            } else if (d1 == d2) {
                return 0;
            } else {
                return 1;
            }
        }).peek(jijinDO1 -> {
            jijinDO1.setGszzl(jijinDO1.getGszzl() + "%");
        }).collect(Collectors.toList());

        JijinDO jijinDO = new JijinDO();
        jijinDO.setEstimate(String.format("%.2f", zero));
        all.add(jijinDO);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("data", all);
        return map;
    }

    @PostMapping("/delete")
    public Boolean delete(String code) {
        if (code == null && code.equals("")) {
            return Boolean.FALSE;
        }
        jijinRepository.deleteByCode(code);
        return Boolean.TRUE;
    }


    @PostMapping("/edit")
    public Boolean edit(String code, BigDecimal share, BigDecimal price) {
        jijinRepository.updateByCode(share, price, code);
        return Boolean.TRUE;
    }
}
